<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <link rel="stylesheet" href="./css/login.css">
</head>
<body>
    <div id="container">
        <div class="login-box">
            <div class="login-box-top">
                <h3>宿舍管理系统</h3>
            </div>
            <div class="login-box-bottom">
                <div class="username">
                    <div class="username-left">
                        <img src="./images/user.png" alt="">
                    </div>
                    <div class="username-right">
                        <input type="text" name="" id="user" placeholder="用户名">
                    </div>
                    
                </div>
                <div class="pasd">
                    <div class="pasd-left">
                        <img src="./images/psd.png" alt="">

                    </div>
                    <div class="pasd-right">
                        <input type="password" name="" id="psd" placeholder="密码">
                    </div>
                </div>
                <div class="choice">
                    <label ></label>
                    <label>
                        学生：
                        <input type="radio" name="jurisdiction" id="students" value="学生"
                            checked>
                    </label>
                    <label>
                        管理员：
                        <input type="radio" name="jurisdiction" id="admin" value="管理员">
                    </label>
                </div>
                <button class="tologin">登录</button>
                <p>学生账号： 101 ，密码： 101</p>
                <p>普通管理员账号： 202 ，密码： 202</p>
                <p>超级管理员账号： 201 ， 密码： 201</p>
            </div>
        </div>
    </div>
    <script src="./js/cookie.js"></script>
    <script src="./js/jquery-3.6.0.min.js"></script>
    <script>
        //获取元素
    let oUser=document.querySelector('#user')
    let oPsd=document.querySelector('#psd')
    let oLogin=document.querySelector('.tologin')
    let oStudent=document.querySelector('#students')
    let oAdmin=document.querySelector('#admin')
    oLogin.onclick=()=>{
        setCookie('upeople',oUser.value)
        if(oStudent.checked){

            $.ajax({
                url:'/tologin',
                data:{
                    s_user:oUser.value,
                    s_pass:oPsd.value
                },
                method:'post',
                success:(res)=>{
                    
                    if(res.msg=='登录成功'){
                        alert('登陆成功！欢迎同学')
                        window.location.href='/students.html'
                    }else{
                        alert('用户名或者密码错误！请联系管理员！')
                    }
                    
                }
                
            })
        }else if(oAdmin.checked){
            $.ajax({
                url:'/tologinAdmin',
                data:{
                    a_user:oUser.value,
                    a_pass:oPsd.value
                },
                method:'post',
                success:(res)=>{
                    
                    if(res.msg=='登录成功'){
                        res.data.forEach(v => {
                        if(v.a_status==0){
                                alert('登录成功！尊敬的超级管理员！')
                                window.location.href='/bigadmin.html'
                        }else{
                                alert('登录成功！尊敬的管理员！')
                                window.location.href='/admin.html'
                        }
                    });
                    
                    }else{
                        alert('用户名或者密码错误！请联系管理员！')
                    }
                }
                
            })
        }
    }
    </script>
</body>
</html>